package com.kt.mes.mqtt;

import com.alibaba.fastjson.JSON;
import com.kt.mes.domain.*;
import com.kt.mes.opc.OpcClientUtil;
import com.kt.mes.service.*;
import com.kt.mes.util.MqttUtil;
import com.kt.mes.util.OpcUtil;
import com.ruoyi.common.utils.http.HttpUtils;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component("MqttInterface")
public class MqttInterface {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private IOpcService opcService;
    @Autowired
    private IStockBinService stockBinService;
    @Autowired
    private IProductStoreListService productStoreListService;




    //根据功能编号匹配操作
    public void funOperate(String fun_site,ToMes toMes){

        String fun=fun_site.substring(0,3); //功能号
        String site=fun_site.substring(3); //站点号

        logger.info("功能编号:"+fun+" 站点编号:"+site);


        Map<String,Object> msg=new HashMap<>();
        msg.put("station","gw"+site);



        OpcNode opcNode=null;
        if(site.equals("01")){
            opcNode=new OpcNode(1);
        } else{
            opcNode=new OpcNode(2);
        }

        //重置数据
        ToMes toMesReset=new ToMes();
        toMesReset.setoId("0");
        List<Map<String, Object>> list_res=   opcService.opcNodeToMap(opcNode,toMesReset);



        switch (fun){
            case "101":
                logger.info("询问订单");
                FromMes fromMes = new FromMes();
                if(site.equals("04")){
                    fromMes= opcService.findWorkPlanByTray(site,Integer.valueOf(toMes.getTrayId()),false);
                }else{
                    fromMes= opcService.findWorkPlanByTray(site,Integer.valueOf(toMes.getTrayId()),true);
                }

                if (fromMes.getoId().equals("0000")) {
                    //没有订单 重置ToMes
                    fromMes.setoId("0002");
                    msg.put("nodes",list_res);
                    MqttUtil.pushData("to/station/write",2, JSON.toJSONString(msg));

                }
               // System.out.println(fromMes);
                //转成node节点
                List<Map<String, Object>> list=   opcService.opcNodeToMap(opcNode,fromMes);

                msg.put("nodes",list);


               MqttUtil.pushData("to/station/write",2, JSON.toJSONString(msg));

                break;
            case "102":
                logger.info("开始运行");
                //2.修改状态
                opcService.updateWpAndWpiAndOp(toMes);
                break;

            case "103":
                logger.info("完成操作");
                //1.读数据

                opcService.finWpAndWpiAndOp(toMes);
                //3.重置信息
                msg.put("nodes",list_res);
                MqttUtil.pushData("to/station/write",2, JSON.toJSONString(msg));

                //如果是1号站点 需要从料仓取料
                if(site.equals("01")){
                    stockBinService.outStockBinByWpi(toMes);

                }
                //如果是6号站点 需要入成品库
                if(site.equals("06")){
                    //库位号
                    //System.out.println(toMes);
                   productStoreListService.takeInPS(Long.valueOf(toMes.getAreaNum()),toMes.getoId());

                }

                break;
            case "105":
                logger.info("开始录歌");
                //1.读数据

                //2.回数据 录歌成功！
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                FromMes fromMesMp3 = opcService.findParam(toMes);
                if (opcService.addFileToUpan(fromMesMp3.getParam01(), "0001.mp3")) {
                    System.out.println("录歌完成");
                }
                break;
            case "106":
                logger.info("开始打印");
                //1.读数据

                //2. 开始控制激光打印机使用 httpClient 并查找变量
                FromMes fromMes1 = opcService.findParam(toMes);
               // System.out.println(fromMes1);
                try {
                  // opcService.printLight("文本",fromMes1.getParam01());
//                    opcService.printLight("文本1",toMes.getoId()+"-"+toMes.getOposId());

                   String res= HttpUtils.sendPost("http://192.168.43.1/changetext_p.php","labelName=文本&content="+fromMes1.getParam01());

                    System.out.println(res);
                    Thread.sleep(2000);


                    String res2=HttpUtils.sendPost("http://192.168.43.1/changetext_p.php","labelName=文本1&content="+toMes.getoId()+"-"+toMes.getOposId());

                    System.out.println(res2);

                } catch (Exception e) {
                    System.out.println("打印机出问题");
                }
                break;
            case "110":
                logger.info("不合格完成");
                //1.读数据
                opcService.finWpAndWpiAndOp(toMes);
                opcService.finAndUnWpAndWpiAndOp(toMes);
                //3.重置信息
                msg.put("nodes",list_res);
                MqttUtil.pushData("to/station/write",2, JSON.toJSONString(msg));
                //3.入废品库
                if(site.equals("02")){


                }
                break;

        }
        //
        Map<String,Object> nodeMap_fun_site_FROMMES=new HashMap<>();
        //功能号
        nodeMap_fun_site_FROMMES.put("id",opcNode.node_fun_site_FROMMES.toParseableString());
        nodeMap_fun_site_FROMMES.put("value",fun_site);
        List<Map<String, Object>> list_fun=new ArrayList<>();
        list_fun.add(nodeMap_fun_site_FROMMES);

        msg.put("nodes",list_fun);
        MqttUtil.pushData("to/station/write",2, JSON.toJSONString(msg));
    }

}
